{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "%matplotlib inline\n",
    "import matplotlib\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import dateutil\n",
    "import analysis\n",
    "\n",
    "packets = analysis.parse_packet_file('data/all.txt')\n",
    "packets += analysis.parse_packet_file('data/temp_basals.txt')\n",
    "\n",
    "# Select valid packets of a particular length\n",
    "packets = filter(lambda x: x.is_valid() and x.body_len == 10, packets)\n",
    "\n",
    "# Uncomment if you want to work with less data (faster while developing)\n",
    "#packets = packets[0:500]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "cracked_bits_dict = analysis.build_bitdiff_dictionary(packets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['116',\n",
       " '117',\n",
       " '118',\n",
       " '119',\n",
       " '120',\n",
       " '121',\n",
       " '122',\n",
       " '123',\n",
       " '124',\n",
       " '125',\n",
       " '134',\n",
       " '135',\n",
       " '3',\n",
       " '38',\n",
       " '39',\n",
       " '4',\n",
       " '42',\n",
       " '43',\n",
       " '44',\n",
       " '45',\n",
       " '5',\n",
       " '6',\n",
       " '66',\n",
       " '67',\n",
       " '7',\n",
       " '83',\n",
       " '84',\n",
       " '85',\n",
       " '86',\n",
       " '87',\n",
       " '88',\n",
       " '89',\n",
       " '90',\n",
       " '91',\n",
       " '92']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b1 = [key for key in cracked_bits_dict.keys() if len(key.split(':')) == 1]\n",
    "b1.sort()\n",
    "b1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "33755"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cracked_bits_dict['86']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "missing xors for bits ['116', '117']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['116', '117']\n",
      "missing xors for bits ['116', '117']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['116', '117']\n",
      "missing xors for bits ['84', '117']\n",
      "missing xors for bits ['116', '117']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['116', '117']\n",
      "missing xors for bits ['116', '117']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['116', '117']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['116', '117']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n",
      "missing xors for bits ['84', '116']\n"
     ]
    }
   ],
   "source": [
    "# HERE HERE HERE\n",
    "# This is the algo that is optimizing the dictionary to infer\n",
    "# single bit diffs from larger diffs\n",
    "\n",
    "for key in cracked_bits_dict.keys():\n",
    "    key_values = key.split(':')\n",
    "    if len(key_values) != 4:\n",
    "        continue\n",
    "    dc = cracked_bits_dict[key]\n",
    "    remaining_bits = []\n",
    "    #print \"-\" * 80\n",
    "    #print \"Working on %s -> 0x%04x\" % (key, dc)\n",
    "    for b in key.split(':'):\n",
    "        if b in b1:\n",
    "            dc = dc ^ cracked_bits_dict[b]\n",
    "            #print \"dc now 0x%04x\" % dc\n",
    "        else:\n",
    "            remaining_bits.append(b)\n",
    "        \n",
    "    if len(remaining_bits) == 0:\n",
    "        #print \"removing key\"\n",
    "        del cracked_bits_dict[key]\n",
    "    elif len(remaining_bits) == 1:\n",
    "        del cracked_bits_dict[key]\n",
    "        print \"discovered xor for bit %s\" % remaining_bits[0]\n",
    "        cracked_bits_dict[remaining_bits[0]] = dc\n",
    "    else:\n",
    "        print \"missing xors for bits %s\" % remaining_bits\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "xors = [cracked_bits_dict[key] for key in cracked_bits_dict.keys() if len(key.split(':')) == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "v = [\"0x%04x\" % x for x in xors]\n",
    "pd.Series(v).value_counts()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
